package display;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Hashtable;
import java.util.Vector;

import logger.ProcessLogger;



import utility.Input;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.FileUtility;
//import file.SearchRecord;

public class FiledList implements Display
{
	final String FIELDLIST="Fieldlist";
	String id;
	DBManager Db;
	//SearchRecord Sr;
	RecordSearch ps;
	GetIdName gid ;	
	
	String path;
	public Vector<Field> field_vector= new Vector<Field>();
	String FIELD="Field";
	public void setId(String id,DBManager Db)
	{
		 this.id = id; 
		 this.Db = Db;
		// path = Input.MASTERREPORTSPATH.concat(id+"//"+id+"_mid"+Input.FILRFORMAT);
		 //Sr = new SearchRecord(path);
		 ps = new RecordSearch(Db);
		 gid = new GetIdName(Db);
		 initializeData();
		 
   }
	public void initializeData()
	{
		try 
		{
			Hashtable<Object, Object>map =new Hashtable<Object, Object>();
			//RandomAccessFile file= new RandomAccessFile(path,"r");
			//FileUtility Fu =  new FileUtility(file);
			map.put("pid",gid.getId(FIELDLIST));
			map.put("mid", id);
			map.put("td", "null");
			/*String list[]= Sr.getRecordSet(map)[0].split("\t");
			Sr.fileClose();*/
			ps.setConditionMap(map);
			String list[] = ps.getArrayFromResultSet();
			int index_pv =Column.pv_index-1;
			String splitlist[] = list[0].split("\t");
			id = splitlist[index_pv];
			//file.close();
		//	path = Input.MASTERREPORTSPATH.concat(id+"//"+id+"_mid"+Input.FILRFORMAT);
				
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public void get_Object()
	{
		Hashtable<Object,Object> map = new 	 Hashtable<Object,Object>();
		map.put("mid", id);
		map.put("td", "null");
		try {
			/* Sr = new SearchRecord(path);
			 Sr.fileOpen();*/
			//RandomAccessFile file1 = new RandomAccessFile(path,"r");
		//	FileUtility Fu =  new FileUtility(file1);
			System.out.println(map);
			/*String line[] = Sr.getRecordSet(map);
			Sr.fileClose();*/
			
			ps = new RecordSearch(Db);
			ps.setConditionMap(map);
			String line[] = ps.getArrayFromResultSet();
			
			int index_pid =Column.pid_index-1;			
			int index_pv = Column.pv_index-1;
			int index_ref =Column.ref_index-1;
			
			try {
				ProcessLogger proc = new ProcessLogger(Db);
				line = proc.sortArrayIndex(line, index_ref);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		
			String fieldId=gid.getId(FIELD);
			for(int i=0;i<line.length;i++)
			{
				try {
					String SubString[] = line[i].split("\t");
					if(SubString[index_pid].equals(fieldId))
					{
						Field f = new Field();
						//if(SubString[].eq)
						f.setId(SubString[index_pv], Db);
						f.getObject();
						
						field_vector.add(f);
						
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
			for(int i=0;i<field_vector.size();i++)
			System.out.println("THe vector is "+field_vector.get(i).Result);
			//file1.close();
			
		
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	
	public String[] generatFieldlist()
	{
		String [] Result = new String[field_vector.size()];
		for(int i=0;i<field_vector.size();i++)
			 Result[i]=field_vector.get(i).Result.get("Text").toString();
		return Result;
	}
	
	public static void main(String[] args) {
		FiledList fl =new FiledList();
		DBManager db = new DBManager();
		db.getConnect();
		fl.setId("5677",db);
		fl.get_Object();
	}
	public Hashtable<Object, Object> getObject() {
			return null;
	}
}
